﻿using System;
using System.Collections.Generic;
using System.Web.Mvc;
using System.Web;

namespace Parichay.Domain.Models
{
    public class ImageResult : ActionResult
    {
        public String ContentType { get; set; }
        public byte[] ImageBytes { get; set; }
        public String SourceFilePath { get; set; }
        public string FileName { get; set; }


        //This is used for times where you have a physical location
        public ImageResult(String sourceFilename, String contentType,string fileName)
        {
            SourceFilePath = sourceFilename;
            ContentType = contentType;
            FileName = fileName;
        }

        //This is used for when you have the actual image in byte form
        //  which is more important for this post.
        public ImageResult(byte[] sourceStream, String contentType,string fileName)
        {
            ImageBytes = sourceStream;
            ContentType = contentType;
            FileName = fileName;
        }

        public override void ExecuteResult(ControllerContext context)
        {
            var response = context.HttpContext.Response;
            response.Clear();
            response.Cache.SetCacheability(HttpCacheability.NoCache);
            response.ContentType = ContentType;

            if (FileName.EndsWith(".pdf", StringComparison.InvariantCultureIgnoreCase))
                response.AppendHeader("Content-Disposition", "inline; filename =\"" + FileName + "\"");
            else
                response.AppendHeader("Content-Disposition", "attachment; filename =\"" + FileName + "\"");

            //Check to see if this is done from bytes or physical location
            //  If you're really paranoid you could set a true/false flag in
            //  the constructor.
            if (ImageBytes != null)
            {
                var stream = new System.IO.MemoryStream(ImageBytes);
                stream.WriteTo(response.OutputStream);
                stream.Dispose();
            }
            else
            {
                response.TransmitFile(SourceFilePath);
            }
        }

        /// <summary>
        /// Reads and Input Stream to Byte Array
        /// </summary>
        /// <param name="inputStream"></param>
        /// <returns></returns>
        public Byte[] readInputStreamToBytes(System.IO.Stream inputStream)
        {
            Byte[] destination = new Byte[inputStream.Length];
            inputStream.Position = 0;
            inputStream.Read(destination, 0, (int)inputStream.Length);
            return destination;
        }

        /// <summary>
        /// Reads a file location into a Byte Array
        /// </summary>
        /// <param name="sFilePhysicalPath">Provide the physical path to file like "C:\Users\Am\Documents\Visual Studio 2008\WebSites\tester\some_image.jpeg"</param>
        /// <returns></returns>
        public byte[] FileToArray(string sFilePhysicalPath)
        {
            System.IO.FileStream fs = new System.IO.FileStream(sFilePhysicalPath,
                System.IO.FileMode.Open, System.IO.FileAccess.Read);
            System.IO.BinaryReader br = new System.IO.BinaryReader(fs);
            Byte[] bytes = br.ReadBytes((Int32)fs.Length);
            br.Close();
            fs.Close();
            return bytes;
        }

        /// <summary>
        /// Retrieves Physical Application Path for a location
        /// For example "C:\Users\Am\Documents\Visual Studio 2008\WebSites\tester\some_image.jpeg" for the path "some_image.jpeg"
        /// </summary>
        /// <param name="path">Physical Path to be appended to PhysicalApplicationPath  e.g.: "\Uploads\some_image.jpeg"</param>
        /// <returns></returns>
        public string GetPhysicalLocation(string path)
        {
            return (System.Web.HttpContext.Current.Request.PhysicalApplicationPath + path);
        }

        public string GetContentTypeFromExtension(string fileExtension)
        {
            switch (fileExtension)
            {
                case ".txt":
                    return "text/plain";
                case ".doc":
                    return "application/ms-word";
                case ".xls":
                    return "application/vnd.ms-excel";
                case ".gif":
                    return "image/gif";
                case ".jpg":
                case "jpeg":
                    return "image/jpeg";
                case ".bmp":
                    return "image/bmp";
                case ".wav":
                    return "audio/wav";
                case ".ppt":
                    return "application/mspowerpoint";
                case ".dwg":
                    return "image/vnd.dwg";
                default:
                    return "application/octet-stream";
            }
        }
    }
}